package com.lehecai.core.lottery.fetcher.jcgyj.impl.worker;

import com.lehecai.core.exception.FetchFailedException;
import com.lehecai.core.lottery.JczqRaceStatus;
import com.lehecai.core.util.CharsetConstant;
import com.lehecai.core.util.CoreFetcherUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 竞彩猜冠军数据抓取抽象类
 * @author qatang
 *
 */
public abstract class AbstractJcgjFetchWorkerOfficial {

    protected static final Logger logger = LoggerFactory.getLogger(AbstractJcgjFetchWorkerOfficial.class);

    protected final static String fetchAllUrl = "http://i.sporttery.cn/rank_calculator/get_list";

    protected static Map<String, String> phaseMap = new HashMap<String, String>();

    static {
        phaseMap.put("2014001", "52562");
        phaseMap.put("2014002", "52563");
    }

    static JSONArray fetchAll(String phase) throws Exception {

        if (phase == null || !phaseMap.containsKey(phase)) {
            return null;
        }

        Map<String, String> requestMap = new HashMap<String, String>();
        requestMap.put("tid[]", phaseMap.get(phase));
        requestMap.put("pcode[]", "chp");


        Map<String, String> requestHeader = new HashMap<String, String>();
        requestHeader.put("Referer", "http://info.sporttery.cn/rankodds/rank.htm");
        requestHeader.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Safari/537.36");

        String pageSource = CoreFetcherUtils.URLGetWithHeaderParams(fetchAllUrl, requestHeader, requestMap, CharsetConstant.CHARSET_UTF8);
        if (pageSource == null || pageSource.indexOf("404 Not Found") > 0) {
            logger.error("抓取竞彩冠军基础数据出错");
            return null;
        }

        JSONObject jsonObject = null;
        try {
            jsonObject = JSONObject.fromObject(pageSource);
        } catch (Exception e) {
            logger.error("抓取结果转换为json对象错误，jsonStr={}", pageSource);
        }
        if (jsonObject == null) {
            return null;
        }
        String raceStr = jsonObject.getJSONArray("data").getJSONObject(0).getString("data");

        String[] raceArray = StringUtils.split(raceStr, "|");
        if (raceArray == null || raceArray.length == 0) {
            throw new FetchFailedException("通用竞彩猜冠军赛程官方抓取实现类抓取失败：按照|拆分赛程后结果数组为空");
        }

        JSONArray resultArray = new JSONArray();
        for (String s : raceArray) {
            JSONObject raceObject = new JSONObject();
            String[] propertyArray = StringUtils.split(s, "-");
            raceObject.put("matchNum", Integer.valueOf(propertyArray[0]) + "");
            raceObject.put("officialNum", propertyArray[0]);
            raceObject.put("team", propertyArray[1]);
            if (propertyArray[2].equals("开售")) {
                raceObject.put("status", JczqRaceStatus.OPEN.getValue());
            } else {
                raceObject.put("status", JczqRaceStatus.CLOSE.getValue());
            }
            raceObject.put("sp", propertyArray[3]);

            resultArray.add(raceObject);
        }

        return resultArray;
    }
}
